# __author__ : htzs
# __time__   : 19-6-14 上午11:52

import datetime


def select_data(request, warn_list):
    check_value = request.GET.get('check_value', '')
    check_dev = request.GET.get('check_dev', '')
    check_manage = request.GET.get('check_manage', '')
    check_day = request.GET.get('check_day', '')
    check_alarm = request.GET.get('check_alarm', '')
    times = request.GET.get('times', '')
    is_sum = request.GET.get('is_sum', '')
    # 判断任务筛选值
    if times:
        start_time = times[:10]
        end_time = times[13:23]
        end_time = datetime.datetime.strptime(end_time, '%Y-%m-%d') + datetime.timedelta(days=1)
        warn_list = warn_list.filter(add_time__range=(start_time, end_time))
    if len(check_manage) == 1:
        warn_list = warn_list.filter(warn_manage=int(check_manage))
    if len(check_manage) == 2:
        warn_list = warn_list.filter(warn_manage=int(check_manage))
    # 判断预警筛选值
    if len(check_value) == 1:
        warn_list = warn_list.filter(warn_type=int(check_value))
    if is_sum:
        warn_list = warn_list
    else:
        if check_value == '':
            warn_list = warn_list.filter(warn_type=1)
    # 判断事件类型
    if len(check_alarm) == 1:
        warn_list = warn_list.filter(alarm_score=int(check_alarm))
    if is_sum:
        warn_list = warn_list
    else:
        if check_alarm == '':
            warn_list = warn_list.filter(alarm_score=1)
    # 判断时间筛选值
    if len(check_day) == 1:
        warn_list = warn_list.filter(is_day=int(check_day))
    # 判断监控点筛选值
    if len(check_dev) == 1:
        warn_list = warn_list.filter(map_id=int(check_dev))
    elif len(check_dev) > 0:
        try:
            check_dev = list(eval(check_dev))
            warn_list = warn_list.filter(map_id__in=check_dev)
        except Exception:
            check_dev = eval(check_dev)
            warn_list = warn_list.filter(map_id=check_dev)
    return warn_list
